<extend name="Layout/ins_popup" />
<block name="content">
	<div class="panel panel-default ">
		<div class="panel-heading clearfix">
			<div class="pull-left">
				<label>
					<input class="ace" type="radio" id="rb_dgp" name="type" value="dgp" >
					<span class="lbl">部门级别-职位</span> </label>

				<label>
					<input class="ace" type="radio" id="rb_dp" name="type" value="dp" >
					<span class="lbl">部门-职位</span> </label>

				<label>
					<input class="ace" type="radio" id="rb_dept" name="type" value="dept"  >
					<span class="lbl">部门</span> </label>

				<label>
					<input class="ace" type="radio" id="rb_emp" name="type" value="emp"  >
					<span class="lbl">人员</span> </label>
			</div>
			<div class="pull-right">
				<a  onclick="save();" class="btn btn-sm btn-primary">确定</a>
				<a  onclick="myclose();" class="btn btn-sm btn-default">关闭</a>
			</div>
		</div>
		<div class="panel-body">
			<div class="col-28 pull-left">
				<div class="">
					<b class="hidden dgp">部门级别</b><b class="hidden dp dept emp">部门</b>
				</div>
				<div class="popup_tree_menu">
					<div id="dept_grade" style="width:100%;height:195px;" class="dgp hidden">
						{$list_dept_grade}
					</div>
					<div id="dept" style="width:100%;height:195px;" class="dp hidden dept">
						{$list_dept}
					</div>
					<div id="emp" style="width:100%;height:195px;" class="emp hidden">
						{$list_dept}
					</div>
				</div>
				<div>
					<b class="hidden dgp dp ">职位</b>
					<b class="hidden emp">人员</b>
				</div>
				<div id="position" class="hidden dgp dp " style="width:100%;height:195px;">
					{$list_position}
				</div>
				<div id="addr_list" class="hidden  emp" style="width:100%;height:195px;"></div>
			</div>
			<div class="col-34 pull-right">
				<div>
					<b style="padding-left: 60px;">审批</b><span id="rc_count"></span>
				</div>
				<div class="clearfix" style="margin-bottom: 15px;">
					<label class="col-4 pull-left text-right" ><a onclick="add_address('rc');" class="btn btn-sm btn-primary"> <i class="fa fa-angle-double-right"></i> </a> </label>
					<div class="col-28 pull-right">
						<div id="rc" style="width:100%;height:195px;overflow:hidden">
							<select size="6" style="height:100%;width:100%;"></select>
						</div>
					</div>
				</div>
				<div>
					<b  style="padding-left: 60px;">协商</b><span id="cc_count"></span>
				</div>
				<div class="clearfix">
					<label class="col-4 pull-left text-right" ><a onclick="add_address('cc');" class="btn btn-sm btn-primary"> <i class="fa fa-angle-double-right"></i></a></label>
					<div class="col-28 pull-right">
						<div id="cc" style="width:100%;height:195px;overflow:hidden">
							<select size="6" style="height:100%;width:100%;"></select>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</block>
<block name="js">
	<script type="text/javascript">
		function save() {
			$("#confirm_wrap,#consult_wrap,#refer_wrap", parent.document).hide();
			$("#confirm_wrap .address_list,#consult_wrap .address_list,#refer_wrap .address_list", parent.document).html("");

			$("#rc select option").each(function(i) {
				emp_no = $(this).val();
				name = jQuery.trim($(this).text());
				html_string = conv_inputbox_item(name, emp_no);
				$("#confirm_wrap .address_list", parent.document).append(html_string);
			});
			$("#cc select option").each(function(i) {
				emp_no = $(this).val();
				name = jQuery.trim($(this).text());
				html_string = conv_inputbox_item(name, emp_no);
				$("#consult_wrap .address_list", parent.document).append(html_string);
			});
			$("#bcc select option").each(function(i) {
				emp_no = $(this).val();
				name = jQuery.trim($(this).text());
				html_string = conv_inputbox_item(name, emp_no);
				$("#refer_wrap .address_list", parent.document).append(html_string);
			});

			$("#confirm_wrap span a", parent.document).remove();
			$("#confirm_wrap .address_list span", parent.document).append("<b><i class=\"fa fa-arrow-right\"></i></b>");
			$("#confirm_wrap .address_list span b:last", parent.document).remove();

			$("#consult_wrap span a", parent.document).remove();
			$("#consult_wrap .address_list span", parent.document).append("<b><i class=\"fa fa-arrow-right\"></i></b>");
			$("#consult_wrap .address_list span b:last", parent.document).remove();

			$("#confirm_wrap,#consult_wrap,#refer_wrap", parent.document).show();
			myclose();
		}

		// 显示AJAX 读取的数据
		function showdata(result) {

			$("#addr_list").html("");
			for (s in result.data) {
				var id = result.data[s].id;
				var position_name = result.data[s].position_name;
				var emp_no = result.data[s].emp_no;
				var email = result.data[s].email;
				var name = result.data[s].name;
				var name = name + "/" + position_name + "&lt;" + email + "&gt;";
				var html_string = conv_address_item(name,emp_no);
				$("#addr_list").append(html_string);
			}
		}


		$(document).ready(function() {
			$("#rb_{$type}").prop('checked', true);
			// 选择用户默认选择的类型

			$(".{$type}").removeClass("hidden");

			$("input[name='type']").on('click', function() {
				$("input[name='type']").each(function() {
					$("." + $(this).val()).addClass("hidden");
				});
				$("." + $(this).val()).removeClass("hidden");
			});

			$("#emp .tree_menu  a").click(function() {
				$(".emp .tree_menu a").attr("class", "");
				var type = $("input[name='type']:checked").val();
				$(this).addClass("active");
				sendAjax("{:U('read')}", "type=" + type + "&id=" + $(this).attr("node"), function(data) {
					showdata(data);
				});
				return false;
				//禁止连接生效
			});

			$("#dept .tree_menu  a").click(function() {
				$("#dept .tree_menu a").each(function() {
					$(this).attr("class", "");
				});
				$(this).addClass("active");

				var type = $("input[name='type']:checked").val();

				return false;
				//禁止连接生效
			});

			$("#dept_grade .tree_menu a").click(function() {
				$("#dept_grade .tree_menu a").each(function() {
					$(this).attr("class", "");
				});
				$(this).addClass("active");

				var type = $("input[name='type']:checked").val();

				return false;
				//禁止连接生效
			});

			$("#position .tree_menu a").click(function() {
				$("#position .tree_menu a").each(function() {
					$(this).attr("class", "");
				});
				$(this).addClass("active");

				var type = $("input[name='type']:checked").val();

				return false;
				//禁止连接生效
			});

			$("#addr_list a").on("dblclick", function() {
				$text = $(this).text();
				$val = $(this).parents("label").find("input").val();
				if ($("#rc select option[value='" + $val + "']").val() == undefined) {
					$("<option></option>").val($val).text($text).appendTo("#rc select");
					$("#rc_count").text("(" + $("#rc select option").length + ")");
				};
			});

			$("#rc  select").on("dblclick", function() {
				$(this).find("option:selected").remove();
				$("#rc_count").text("(" + $("#rc select option").length + ")");
			});

			$("#cc  select").on("dblclick", function() {
				$(this).find("option:selected").remove();
				$("#cc_count").text("(" + $("#cc select option").length + ")");
			});
						
		});
		//最终确认

		function add_address(name) {
			var type = $("input[name='type']:checked").val();
			switch(type) {
			case "dgp":
				$dept_grade_id = $("#dept_grade a.active").attr("node");
				$dept_grade_name = $("#dept_grade a.active span").text();

				if ($dept_grade_id == undefined) {
					ui_error("请选择部门级别");
					return false;
				}
				$position_id = $("#position a.active").attr("node");
				$position_name = $("#position a.active span").text();

				if ($position_id == undefined) {
					ui_error("请选择职位");
					return false;
				}
				$text = $dept_grade_name + "-" + $position_name;
				$val = "dgp_" + $dept_grade_id + "_" + $position_id;
				$("<option></option>").val($val).text($text).appendTo("#" + name + " select");
				$("#" + name + "_count").text("(" + $("#" + name + " select option").length + ")");
				break;
			case "dp":
				$dept_id = $("#dept a.active").attr("node");
				$dept_name = $("#dept a.active span").text();

				if ($dept_id == undefined) {
					ui_error("请选择部门");
					return false;
				}
				$position_id = $("#position a.active").attr("node");
				$position_name = $("#position a.active span").text();

				if ($position_id == undefined) {
					ui_error("请选择职位");
					return false;
				}
				$text = $dept_name + "-" + $position_name;
				$val = "dp_" + $dept_id + "_" + $position_id;
				$("<option></option>").val($val).text($text).appendTo("#" + name + " select");
				$("#" + name + "_count").text("(" + $("#" + name + " select option").length + ")");
				break;
			case "dept":
				$dept_id = $("#dept a.active").attr("node");
				$dept_name = $("#dept a.active span").text();

				if ($dept_id == undefined) {
					ui_error("请选择部门");
					return false;
				}
				$text = $dept_name;
				$val = "dept_" + $dept_id;
				$("<option></option>").val($val).text($text).appendTo("#" + name + " select");
				$("#" + name + "_count").text("(" + $("#" + name + " select option").length + ")");
				break;

			case "emp":
				$("input:checked[name='addr_id']").each(function() {
					$(this).prop('checked',false);
					$text = $(this).parents("label").text();
					$val = "emp_" + $(this).val();
					if ($("#" + name + " select option[value='" + $val + "']").val() == undefined) {
						$("<option></option>").val($val).text($text).appendTo("#" + name + " select");
						$("#" + name + "_count").text("(" + $("#" + name + " select option").length + ")");
					};
				});
				break;
			}
		}
	</script>
</block>
